package com.gscitysfy.cus.drain.modules.common.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gscitysfy.cloudfrmwk.assistant.domain.Result;
import com.gscitysfy.cloudfrmwk.portal.feign.DictionaryService;
import com.gscitysfy.cloudfrmwk.portal.feign.DistrictService;
import com.gscitysfy.cloudfrmwk.portal.feign.UserService;
import com.gscitysfy.cloudfrmwk.portal.vo.*;
import com.gscitysfy.cus.drain.modules.common.entity.Tree;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 公共数据字典接口
 * @author Administrator
 */
@RestController
@RequestMapping("/portal/")
@Slf4j
@Tag(name = "字典常量接口", description = "获取数据字典、用户基本信息")
public class PortalController {
    @Resource
    private DictionaryService dictionaryService;

    @Resource
    private UserService userService;

    @Resource
    private DistrictService districtService;

    /**
     * 未定义标识
     */
    private static final String UNDEFINED = "undefined";

    /**
     * 根据编码查找数据字典
     * @param code
     * @return
     */
    @GetMapping("/getDictionaryByCode")
    public Result<List<DictionaryItemVO>> getDictionaryByCode(@RequestParam String code, @RequestParam(required = false) String sysFlag) {
        Result<List<DictionaryItemVO>> dictionary = dictionaryService.getDictionary(code);
        if(StringUtils.isNotEmpty(sysFlag) && !UNDEFINED.equals(sysFlag)){
            List<DictionaryItemVO> collect = dictionary.getData().stream()
                    .filter(item-> StringUtils.isNotEmpty(item.getSystemFlag()))
                    .filter(item -> Arrays.asList(item.getSystemFlag().split(StrUtil.COMMA)).contains(sysFlag))
                    .collect(Collectors.toList());
            dictionary.setData(collect);
        }
        return dictionary;
    }

    /**
     * 根据编码和专项code获取数据字典
     * @param code
     * @param sysFlag
     * @return
     */
    @GetMapping("/getDictionaryByCodeAndSysFlag")
    public Result<List<DictionaryItemVO>> getDictionaryByCodeAndSysFlag(@RequestParam String code, @RequestParam String sysFlag) {
        Result<List<DictionaryItemVO>> dictionary = dictionaryService.getDictionary(code);
        List<DictionaryItemVO> collect = dictionary.getData().stream()
                .filter(item -> sysFlag.equals(item.getSystemFlag())).collect(Collectors.toList());
        dictionary.setData(collect);
        return dictionary;
    }

    /**
     * 根据编码获取所属区域
     * @param code
     * @return
     */
    @GetMapping("/getDistrictSimpleTreeByCode")
    public Result<DistrictVO> getDistrictSimpleTreeByCode(String code) {
        return districtService.getDistrictSimpleTreeByCode(code);
    }

    /**
     * 根据角色编码获取数据
     * @param roleCode
     * @return
     */
    @GetMapping("/getUserByRoleCodePage")
    public Result<Page<UserInfoVO>> getUserByRoleCodePage(Page<UserInfoVO> page, String roleCode, String idCard, String userName) {
        return userService.getUserByRoleCodePage(page, roleCode, idCard, userName);
    }

    /**
     * 获取组织结构树
     */
    @GetMapping("/getOrganizationTree")
    public Result<List<OrganizationVO>> getOrganizationTree() {
        return userService.getOrganizationTree();
    }


    /**
     * 根据用户获取菜单
     */
    @GetMapping("/getUserMenu")
    public  Result<List<MenuVO>> getUserMenu() {
        return userService.getUserMenu();
    }


    /**
     * 获取用户信息
     */
    @GetMapping("/getUserInfo")
    public Result<UserInfoVO> getUserInfo() {
        return userService.getUserInfo();
    }

     /**
     * 获取行政区划树
     */
    @GetMapping("/getDistrictSimpleTree")
    public Result<List<DistrictVO>> getDistrictSimpleTree(String code) {
        return districtService.getDistrictSimpleTree();
    }


    /**
     * 部门人员组织
     * @param
     * @return
     */
    @GetMapping("/getOrganizationUserTree")
    public Result<List<OrganizationVO>> getOrganizationUserTree() {
        return  userService.getOrganizationUserTree();
    }

    /**
     * 获取组织架构树
     * @return
     */
    @GetMapping("/getSimpleOrganizationUserTree")
    public Result<List<Tree>> getSimpleOrganizationUserTree(){
        List<TreeVo> data = userService.getSimpleOrganizationUserTree().getData();
        List<Tree> trees = JSONArray.parseArray(JSON.toJSONString(data), Tree.class);
        checkPerson(trees);
        return Result.content(trees);
    }


    @GetMapping("/getUserList")
    @Operation(summary = "分页获取用户数据")
    @Parameters({
            @Parameter(required = false, name = "size", description = "分页没页条数"), @Parameter(required = false, name = "current", description = "当前页"),
            @Parameter(required = false, name = "name", description = "用户名"), @Parameter(required = false, name = "phoneNumber", description = "用户手机号"),
    })
    public Result<Page<UserInfoVO>> getUserList(Page<UserInfoVO> page, UserInfoVO user){
        return  this.userService.getUserPage(page, user.getName(), user.getUserName(), user.getPhoneNumber(), null);
    }

    /**
     * 监测是否是人
     * @param trees
     */
    private void checkPerson(List<Tree> trees){
        if(!Objects.isNull(trees)){
            trees.forEach(tree ->{
                if(Objects.isNull(tree.getChildren())){
                    tree.setPerson(true);
                }else{
                    tree.setPerson(false);
                    checkPerson(tree.getChildren());
                }
            });
        }
    }


}

